package com.skit.common.base.mvvm

import android.content.pm.ActivityInfo
import android.os.Build
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.skit.common.R
import com.skit.common.event.EventBusLifecycleObserver
import com.skit.common.event.EventMessage
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

abstract class BaseActivity<DB : ViewDataBinding> : AppCompatActivity() {

    @get:LayoutRes
    abstract val layoutResId: Int

    val binding: DB by lazy {
        DataBindingUtil.inflate<DB>(
            layoutInflater,
            layoutResId,
            null,
            false
        ).apply {
            lifecycleOwner = this@BaseActivity
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        setContentView(binding.root)
        EventBusLifecycleObserver(this)
        val window = window
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //如果大于等于21（5.0）
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { //是否大于等于6.0
                if (Build.VERSION.SDK_INT >= 27) { //是否大于等于8.1
                    window.decorView.systemUiVisibility =
                        View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                } else {
                    window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                    window.navigationBarColor = resources.getColor(R.color.black)
                }
            } else {
                window.navigationBarColor = resources.getColor(R.color.black)
                window.statusBarColor = resources.getColor(R.color.statusColor)
            }
        }
        initView()
        initConfig()
        initData()
    }

    abstract fun initData()

    abstract fun initConfig()

    abstract fun initView()

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == android.R.id.home) finish()
        return super.onOptionsItemSelected(item)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    open fun event(event: EventMessage) {

    }
}